﻿/*********************************************************************************/
/**    Sims3Pack Readme Generator                                               **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Sims3PackReadmeGenerator.Sims3PackReader;
using Sims3PackReadmeGenerator.language;

namespace Sims3PackReadmeGenerator.ContentDesciption
{
    /// <summary>
    /// represents all the infos from the xml description in a sims3pack
    /// </summary> 
    class ContentDescription
    {
        #region fields

        private List<DescriptionItem>               _expansionList          = new List<DescriptionItem>();
        private List<DescriptionItem>               _expansionAbbrevList    = new List<DescriptionItem>();
        private List<DescriptionItem>               _stuffList              = new List<DescriptionItem>();
        private List<DescriptionItem>               _stuffAbbrevList        = new List<DescriptionItem>();
        private List<DescriptionLinkItem>           _storeList              = new List<DescriptionLinkItem>();
        private List<string>                        _unresolvedDependencies = new List<string>();
        private List<PackagedBadCCDescription>      _badCCList              = new List<PackagedBadCCDescription>();
        private List<PackagedKnownFileDescription>  _packagedFileList       = new List<PackagedKnownFileDescription>();
        private List<PackagedFileDescription>       _unknownPackagesList    = new List<PackagedFileDescription>();
        private List<Bitmap>                        _thumbnailList          = new List<Bitmap>();

        #endregion

        #region properties

        public string FilePath { get; set; }
        public int BinaryStartPosition { get; set; }       
        public ContentTypes Type { get; set; } 
        public string GameVersion { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public string PackageID { get; set; }
        public PackagedFileDescription PackagedMainFile { get; set; }

        #endregion

        #region constructor

        public ContentDescription()
        {          

            this.PackagedMainFile = null;
        }

        #endregion

        #region methods

        /// <summary>
         /// needed to display a description in a combobox
         /// </summary>
         /// <returns></returns>
        public override string ToString()
        {
            return Path.GetFileName(this.FilePath);
        }
        
        // add items to the according lists        
        #region methods for adding items

        public void addExpansion(DescriptionItem expansion)
        {
            _expansionList.Add(expansion);
        }

        public void addExpansionAbbrev(DescriptionItem expansion)
        {
            _expansionAbbrevList.Add(expansion);
        }
      
        public void addStuffpack(DescriptionItem stuffpack)
        {
            _stuffList.Add(stuffpack);
        }

        public void addStuffpackAbbrev(DescriptionItem stuffpack)
        {
            _stuffAbbrevList.Add(stuffpack);
        }
        
        public void addStoreItem(DescriptionLinkItem storeitem)
        {
            _storeList.Add(storeitem);
        }
       
        public void addUnresolvedDependency(string dependency)
        {
            _unresolvedDependencies.Add(dependency);
        }
       
        public void addBadCCPackedFile(PackagedBadCCDescription packFile)
        {
            _badCCList.Add(packFile);
        }   
     
        public void addThumbnail(Bitmap bmp)
        {
            _thumbnailList.Add(bmp);
        }

        public void addPackedFile(PackagedKnownFileDescription packFile)
        {
            _packagedFileList.Add(packFile);
        }

        public void addUnknownPackedFile(PackagedFileDescription packFile)
        {
            _unknownPackagesList.Add(packFile);
        }

        #endregion
         
        // output methods
        // provide strings in a specified language
        #region output methods

        public string getDisplayName(Languages lang)
        {
            return this.DisplayName;
        }

        public string getDesription(Languages lang)
        {
            return this.Description;
        }

        public string getContentType(Languages lang)
        {
           string type = "";

            //if contenttype is sim, use species information from package meta to display sim / animal types
           if (this.Type == ContentTypes.sim)
           {
               PackagedSimDescription simDesr = this.PackagedMainFile as PackagedSimDescription;
               if (simDesr != null)
               {
                   type = simDesr.getSimSpecies(lang);
               }
           }
           else
           {
               type = ReferenceResolver.ContentTypeToString(this.Type,lang);
           }

           return type;
        }

        public string getPackageID()
        {
            return this.PackageID;
        }

        public string getGameVersion()
        {
            return this.GameVersion;
        }

        public string getFileName()
        {
            return Path.GetFileName(this.FilePath);
        }

        public string getExpansionList(Languages lang, StringListFormats format)
        {
            return getStringFromStringList(_expansionList, lang, format);
        }

        public string getExpansionAbbrevList(Languages lang, StringListFormats format)
        {
            return getStringFromStringList(_expansionAbbrevList, lang, format);
        }

        public string getStuffpackList(Languages lang, StringListFormats format)
        {
            return getStringFromStringList(_stuffList, lang, format);
        }

        public string getStuffpackAbbrevList(Languages lang, StringListFormats format)
        {
            return getStringFromStringList(_stuffAbbrevList, lang, format);
        }

        public string getStoreListString(Languages lang, string seperator)
        {
            string display = "";
          
            if (lang == Languages.de)
            {
                foreach (DescriptionLinkItem listItem in _storeList)
                {
                    display += listItem.DE + seperator;
                }
            }
            else
            {
                foreach (DescriptionLinkItem listItem in _storeList)
                {
                    display += listItem.EN + seperator;                    
                }
            }

            //remove last seperator
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - seperator.Length);
            }

            return display;
        }

        public string getStoreLinkListString(Languages lang, string seperator)
        {
            string display = "";
           
            if (lang == Languages.de)
            {
                foreach (DescriptionLinkItem listItem in _storeList)
                {
                    display += "<a href=\"" + listItem.URL + "\">" + listItem.DE + "</a>" + seperator;
                }
            }
            else
            {
                foreach (DescriptionLinkItem listItem in _storeList)
                {
                    display += "<a href=\"" + listItem.URL + "\">" + listItem.EN + "</a>" + seperator;
                }
            }

            //remove last seperator
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - seperator.Length);
            }

            return display;
        }

        public string[] getStoreList(Languages lang)
        {
            string[] listStoreItems;
            List<string> listTemp = new List<string>();

            if (lang == Languages.de)
            {
                foreach (DescriptionItem item in _storeList)
                {
                    listTemp.Add(item.DE);
                }
            }
            else
            {
                foreach (DescriptionItem item in _storeList)
                {
                    listTemp.Add(item.EN);
                }
            }

            listStoreItems = listTemp.ToArray();
            return listStoreItems;
        }

        public string getKnownPackagesListSimple(Languages lang, string seperator)
        {
            string display = "";

            if (lang == Languages.de)
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += listItem.ItemNameDE + seperator;
                }
            }
            else
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += listItem.ItemNameEN + seperator;
                }
            }

            //letztes Komma und Leerzeichen entfernen
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - seperator.Length);
            }

            return display;
        }

        public string getKnownPackagesListDetails(Languages lang, string seperator)
        {
            string display = "";

            if (lang == Languages.de)
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += listItem.ItemNameDE + " " + names.contentDescriptionBy + ": " + listItem.Creator + "  -  " + listItem.Homepage + seperator;
                }
            }
            else
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += listItem.ItemNameEN + " " + names.contentDescriptionBy + ": " + listItem.Creator + "  -  " + listItem.Homepage + seperator;
                }
            }

            // remove seperator
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - (seperator.Length));
            }
            return display;
        }

        public string getKnownPackagesListLinks(Languages lang, string seperator)
        {
            string display = "";

            if (lang == Languages.de)
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += "<a href=\""+ listItem.Homepage +"\">"+listItem.ItemNameDE + "</a>" + seperator;
                }
            }
            else
            {
                foreach (PackagedKnownFileDescription listItem in _packagedFileList)
                {
                    display += "<a href=\"" + listItem.Homepage + "\">" + listItem.ItemNameDE + "</a>" + seperator;
                }
            }

            //letztes Komma und Leerzeichen entfernen
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - seperator.Length);
            }

            return display;
        }

        public List<Bitmap> getThumbnailList()
        {
            return _thumbnailList;
        }

        public List<PackagedBadCCDescription> getBadCCList()
        {
            return _badCCList;
        }

        public List<PackagedFileDescription> getUnknownPackagesList()
        {
            return _unknownPackagesList;
        }

        #endregion

        #region help methods

        /// <summary>
        /// builds a single string from a given string list
        /// contains all list elemnts seperated by given seperator
        /// </summary>
        /// <param name="list">string list</param>
        /// /// <param name="lang">language</param>
        /// <param name="format">list seperator</param>
        /// <returns>resulting string</returns>
        private string getStringFromStringList(List<DescriptionItem> list, Languages lang, string seperator)
        {
            string display = "";

            if (lang == Languages.de)
            {
                foreach (DescriptionItem listItem in list)
                {
                    display += listItem.DE + seperator;
                }
            }
            else
            {
                foreach (DescriptionItem listItem in list)
                {
                    display += listItem.EN + seperator;
                }
            }            

            //remove last comma and space
            if (display.Length > seperator.Length)
            {
                display = display.Substring(0, display.Length - seperator.Length);
            }

            return display;
        }

        /// <summary>
        /// builds a single string from a given string list
        /// contains all list elements seperated as specified by format
        /// </summary>
        /// <param name="list">string list</param>
        /// <param name="lang">language</param>
        /// <param name="format">list format</param>
        /// <returns>resulting string</returns>
        private string getStringFromStringList(List<DescriptionItem> list, Languages lang, StringListFormats format)
        {
            if (format == StringListFormats.commaSep)
            {
                return getStringFromStringList(list, lang, ", ");
            }
            else if (format == StringListFormats.newline)
            {
                return getStringFromStringList(list, lang, ",\r\n");
            }
            else
            {
                return getStringFromStringList(list, lang, ", ");
            }

        }


        #endregion

        #endregion
    }
}
